import 'dart:convert' as convert;

import 'package:flutter_1800/models/Session.dart';
import 'package:flutter_1800/models/user.dart';
import 'package:shared_preferences/shared_preferences.dart';

class StorageUtil {
  static saveUser(Map map) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("user", convert.jsonEncode(map));
  }

  static clear() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.clear();
  }

  static getApplication(String? id) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? userJson = prefs.getString("application_" + id.toString());
    if (userJson != null && userJson.isNotEmpty) {
      Map<String, dynamic> map = convert.jsonDecode(userJson);
      return map;
    }
    return {};
  }

  static getMessageLatests() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? userJson = prefs.getString("messageLatests");
    if (userJson != null && userJson.isNotEmpty) {
      List list = convert.jsonDecode(userJson);
      return list;
    }
    return [];
  }

  static clearUser() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove("user");
  }

  static getUser() async {
    SharedPreferences prefs = await SharedPreferences
        .getInstance(); // 作用：可以理解为一个本地存储的数据库，我们可以通过这个对象来对数据库进行增删改查的操作
    String? userJson = prefs.getString("user");
    if (userJson != null && userJson.isNotEmpty) {
      Map<String, dynamic> userMap = convert.jsonDecode(userJson);
      User user = User.fromMap(userMap);
      return user;
    }
    return null;
  }

  static getToken() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? secretOpen = prefs.getString("token");

    return secretOpen;
  }

  static saveSessions(List sessions, String account) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List ses = [];
    for (int i = 0; i < sessions.length; i++) {
      Session session = sessions[i];
      ses.add(session.toMap());
    }
    prefs.setString("sessions5_" + account, convert.jsonEncode(ses));
  }

  static getSessions(String account) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? userJson = prefs.getString("sessions5_" + account);
    if (userJson != null && userJson.isNotEmpty) {
      List sessions = convert.jsonDecode(userJson);
      List ses = [];
      for (int i = 0; i < sessions.length; i++) {
        Map session = sessions[i];
        ses.add(Session(
            to_account: session["to_account"],
            account: session["account"],
            content: session["content"],
            avatar: session["avatar"],
            type: session["type"],
            nickname: session["nickname"],
            action: session["action"],
            msg_type: session["msg_type"],
            unread: session["unread"],
            time: session["time"]));
      }
      ses.sort((a, b) => b.time.compareTo(a.time));
      return ses;
    }
    return [];
  }
}
